package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.GodUser;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.service.GodUserService;
import com.javasm.service.impl.GodUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/godCustomer/*")
public class GodUserServlet extends BaseServlet {
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String customerIdStr = req.getParameter("uid");
        String phone = req.getParameter("phone");
        String withdrawDetail = req.getParameter("withdrawDetail");
        String skillOpenStr = req.getParameter("skillOpen");
        String communityIdStr = req.getParameter("communityId");
        Integer uid = null;
        if (customerIdStr != null && !"".equals(customerIdStr)) {
            uid = Integer.parseInt(customerIdStr);
        }
        Integer skillOpen = null;
        if (skillOpenStr != null && !"".equals(skillOpenStr)) {
            skillOpen = Integer.parseInt(skillOpenStr);
        }
        Integer communityId = null;
        if (communityIdStr != null && !"".equals(communityIdStr)) {
            communityId = Integer.parseInt(communityIdStr);
        }
        GodUser inputCustomer = new GodUser(uid, phone, withdrawDetail, communityId, skillOpen);

        GodUserService godUserService = new GodUserServiceImpl();
        int resNum = godUserService.addGodCustomer(inputCustomer);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();


    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String customerIdStr = req.getParameter("uid");
        Integer uid = null;
        if (customerIdStr != null && !"".equals(customerIdStr)) {
            uid = Integer.parseInt(customerIdStr);
        }

        GodUserService godUserService = new GodUserServiceImpl();
        int resNum = godUserService.deleteGodCustomerById(uid);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String customerIdStr = req.getParameter("uid");
        String phone = req.getParameter("phone");
        String withdrawDetail = req.getParameter("withdrawDetail");
        String skillOpenStr = req.getParameter("skillOpen");
        String communityIdStr = req.getParameter("communityId");
        Integer uid = null;
        if (customerIdStr != null && !"".equals(customerIdStr)) {
            uid = Integer.parseInt(customerIdStr);
        }
        Integer skillOpen = null;
        if (skillOpenStr != null && !"".equals(skillOpenStr)) {
            skillOpen = Integer.parseInt(skillOpenStr);
        }
        Integer communityId = null;
        if (communityIdStr != null && !"".equals(communityIdStr)) {
            communityId = Integer.parseInt(communityIdStr);
        }
        GodUser inputCustomer = new GodUser(uid, phone, withdrawDetail, communityId, skillOpen);

        GodUserService godUserService = new GodUserServiceImpl();

        int resNum = godUserService.editGodCustomer(inputCustomer);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
    public void getGodCustomerById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String customerIdStr = req.getParameter("uid");
        Integer uid = null;
        if (customerIdStr != null && !"".equals(customerIdStr)) {
            uid = Integer.parseInt(customerIdStr);
        }
        GodUserService godUserService = new GodUserServiceImpl();
        GodUser godUser = godUserService.getGodCustomerById(uid);
        ReturnResult rrs = null;
        if (godUser != null) {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(), godUser);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageSize = 10;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        String nickname = req.getParameter("nickname");
        String communityName = req.getParameter("communityName");
        String skillOpenStr = req.getParameter("skillOpen");
        Integer skillOpen = null;
        if (skillOpenStr != null && !"".equals(skillOpenStr)) {
            skillOpen = Integer.parseInt(skillOpenStr);
        }
        GodUser inputCustomer = new GodUser();
        inputCustomer.setNickname(nickname);
        inputCustomer.setCommunityName(communityName);
        inputCustomer.setSkillOpen(skillOpen);
        GodUserService godUserService = new GodUserServiceImpl();
        Integer total = godUserService.countGodCustomers(inputCustomer);
        //总页数计算公式 total%pageSize 能不能整除
        //能整除    total/pageSize   页
        //不能整除  total/pageSize+1  页
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        //检查当前要查的页数 是否超过了最大页数
        //如果超过了 则用最后一页查
        //修复bug时 有可能产生新bug
        //功能完成后 需要重新测试 之前测通的功能 可能会与现在的功能产生冲突 造成bug
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<GodUser> customers = godUserService.listGodCustomers(inputCustomer, page, pageSize);
        ReturnResult rrs = null;
        if (customers.size() > 0) {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(),customers);
            PageInfo pageInfo = new PageInfo(page,pageSize,total);
            rrs.setPageInfo(pageInfo);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
}
